package com.jiang.jixiang.ui.fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.jiang.jixiang.R;
import com.jiang.jixiang.api.ApiUrl;
import com.jiang.jixiang.bean.BillingRecordsBean;
import com.jiang.jixiang.bean.UserInfoBean;
import com.jiang.jixiang.databinding.FragmentRemainBinding;
import com.jiang.jixiang.network.CommonalityModel;
import com.jiang.jixiang.network.NetWorkListener;
import com.jiang.jixiang.network.okHttpModel;
import com.jiang.jixiang.tools.PubDiaUtils;
import com.jiang.jixiang.ui.adapter.BillingAdapter;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;
import com.ws.universal.tools.base.BaseFragment;
import com.ws.universal.tools.utils.GsonUtils;
import com.ws.universal.tools.utils.StringUtils;
import com.ws.universal.tools.utils.ToastUtils;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class MyWalletFragment extends BaseFragment implements NetWorkListener, OnRefreshListener, OnLoadMoreListener {
    private FragmentRemainBinding binding;
    private BillingAdapter billingAdapter;
    private BillingRecordsBean billingRecordsBean;

    @Override
    public View getRootView(LayoutInflater inflater, ViewGroup container) {
        binding = FragmentRemainBinding.inflate(inflater, container, false);
        return binding.getRoot();
    }

    @Override
    public void initView() {

        binding.refreshLayoutlive.setOnRefreshListener(this);//加载更多
        binding.refreshLayoutlive.setOnLoadMoreListener(this);//加载更多

        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        binding.rcyData.setLayoutManager(manager);

        billingAdapter = new BillingAdapter();
        binding.rcyData.setAdapter(billingAdapter);

        binding.btnRefundBalance.setChangeAlphaWhenPress(true);
        binding.btnRefundBalance.setOnClickListener(view -> {
            PubDiaUtils.getInstance().showTwoBtnDialog(getContext(), "", getResources().getString(R.string.withdraw_hint), getResources().getString(R.string.cancel), getResources().getString(R.string.yes), new PubDiaUtils.PublicDiaologCallback() {
                @Override
                public void onConfirm() {

                    balanceRefund();
                }

                @Override
                public void onCancel() {


                }
            });
        });


    }


    private void balanceRefund() {
        Map<String, String> params = new HashMap<>();
        okHttpModel.post(ApiUrl.refund_Api, params, ApiUrl.refund_Api_ID, this);
    }

    public void loadData() {
        //showProgressDialog(getActivity(),false);
        Map<String, String> params = new HashMap<>();
        okHttpModel.post(ApiUrl.getUserInfo_Api, params, ApiUrl.getUserInfo_Api_ID, this);
        loadBill();
    }


    public int pageNumber = 1;
    public int pages;

    private void loadBill() {
        Map<String, Object> params = new HashMap<>();
        params.put("page_no", pageNumber);
        params.put("page_size", page_size);
        okHttpModel.postJson(ApiUrl.paymentLog_Api, params, ApiUrl.paymentLog_Api_ID, this);
    }

    @Override
    public void autoData() {

    }

    @Override
    public void onSucceed(JSONObject jsonData, int id, CommonalityModel commonality) {
        stopProgressDialog();
        JSONObject data = jsonData.optJSONObject("data");
        switch (id) {
            case ApiUrl.getUserInfo_Api_ID:
                stopProgressDialog();
                UserInfoBean userInfoBean = GsonUtils.parseJObject(data.toString(), UserInfoBean.class);
                binding.tvBalance.setText(StringUtils.getString(getString(R.string.money), userInfoBean.getPowerbank_balance()));
                break;

            case ApiUrl.paymentLog_Api_ID:
                billingRecordsBean = GsonUtils.parseJObject(data.toString(), BillingRecordsBean.class);
                if (billingRecordsBean != null) {
                    pages = billingRecordsBean.getTotal();
                    if (pageNumber > 1) {
                        for (int i = 0; i < billingRecordsBean.getList().size(); i++) {
                            billingAdapter.getData().add(billingRecordsBean.getList().get(i));
                        }
                        billingAdapter.notifyDataSetChanged();
                    } else {
                        billingAdapter.setList(billingRecordsBean.getList());
                        if (billingAdapter.getData().size() == 0) {
                            binding.inNoData.llNoData.setVisibility(View.VISIBLE);
                        } else {
                            binding.inNoData.llNoData.setVisibility(View.GONE);
                        }
                    }

                }
                break;
            case ApiUrl.refund_Api_ID:
                ToastUtils.showShort(commonality.getMessage());
                binding.tvBalance.setText(StringUtils.getString(getString(R.string.money), "0"));
                //BaseBusData busData = new BaseBusData(BusCode.updateUserInfo);
                //EventBusUtil.postEvent(busData);
                break;


        }
    }

    @Override
    public void onFail(JSONObject jsonData, int id, CommonalityModel commonality) {
        stopProgressDialog();
        switch (id) {
            case ApiUrl.refund_Api_ID:
                ToastUtils.showShort(commonality.getMessage());
                break;
        }
    }

    @Override
    public void onError(int id, Exception e) {
        stopProgressDialog();
        ToastUtils.showShort(e.getMessage());
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        if (refreshLayout != null) {
            pageNumber = 1;
            loadBill();
            refreshLayout.finishRefresh();
            refreshLayout.resetNoMoreData();//setNoMoreData(false);//重置初始状态
        }
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        if (refreshLayout != null) {
            if (pageNumber >= pages) {
                refreshLayout.finishLoadMoreWithNoMoreData();//将不会再次触发加载更多事件
            } else {
                pageNumber++;
                loadBill();
                refreshLayout.finishLoadMore();
            }
        }
    }
}
